//Camera

float3 CameraPosition;
float4x4 LightViewProjection;

//Lights
float4 AmbientColor;

float3 PointLight0Position;
float4 PointLight0Color;
float PointLight0Range;
float4 SpecularColor;
float SpecularPower;
float4 DiffuseColor;

float Attenuation0;
float Attenuation1;
float Attenuation2;

//Shadow map

Texture ShadowMapTexture;


sampler shadowMapSampler = sampler_state
	 { texture = <ShadowMapTexture>; magfilter = LINEAR; minfilter = LINEAR; mipfilter = LINEAR; AddressU = clamp; AddressV = clamp; };


float4 ComputeLighting(float3 normal, float3 worldPosition)
{
	float3 lightDirection = worldPosition - PointLight0Position;
	float distanceToLight = length(lightDirection);
	normal = normalize(normal);

	float diffuseFactor = saturate(dot(normalize(-lightDirection), normal));

	float attenuationFactor;
	if (distanceToLight >= PointLight0Range)
		attenuationFactor = 0;
	else
		attenuationFactor = 1 / (Attenuation2 * distanceToLight * distanceToLight + Attenuation1 * distanceToLight + Attenuation0);

	float4 diffuseColor = PointLight0Color * diffuseFactor * attenuationFactor;

	//calculate Phong components
	float3 eyeVector = normalize(CameraPosition - worldPosition);
	float3 reflectionVector = normalize(reflect(lightDirection, normal));
	float specularFactor = saturate(dot(reflectionVector, eyeVector));
	specularFactor = pow(specularFactor, SpecularPower);
	float4 specularColor = SpecularColor * specularFactor;

	float4 finalColor = saturate(diffuseColor + specularColor + AmbientColor);

	return finalColor;
}


